package com.ruoyi.jkcrData.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.calculate.BezierCurveUtil;
import com.ruoyi.jkcrData.domain.plat.DataJkcrZhchshx;
import com.ruoyi.jkcrData.domain.plat.JkcrZhchshxChange;
import com.ruoyi.jkcrData.mapper.DataJkcrZhchshxMapper;
import com.ruoyi.jkcrData.service.IDataJkcrZhchshxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;


/**
 * 架空乘人重车上行空车下行测试数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-19
 */
@Service
public class DataJkcrZhchshxServiceImpl implements IDataJkcrZhchshxService
{
    @Autowired(required = false)
    private DataJkcrZhchshxMapper dataJkcrZhchshxMapper;

    /**
     * 查询架空乘人重车上行空车下行测试数据
     * 
     * @param zhchshxId 架空乘人重车上行空车下行测试数据主键
     * @return 架空乘人重车上行空车下行测试数据
     */
    @Override
    public DataJkcrZhchshx selectDataJkcrZhchshxByZhchshxId(Long zhchshxId)
    {
        return dataJkcrZhchshxMapper.selectDataJkcrZhchshxByZhchshxId(zhchshxId);
    }

    /**
     * 根据dataTaskId获取数据
     *
     * @param dataTaskId
     */
    @Override
    public Future<List<DataJkcrZhchshx>> selectDataJkcrZhchshxByDataTaskId(Long dataTaskId) {
        List<DataJkcrZhchshx> dataJkcrZhchshxEntities = dataJkcrZhchshxMapper.selectDataJkcrZhchshxByDataTaskId(dataTaskId);
        if (!dataJkcrZhchshxEntities.isEmpty()) {
            dataJkcrZhchshxEntities.forEach(item -> {
                if (item.getSdList() != null) {
                    // 速度曲线数据
                    List<Float> suduVelList = JSON.parseArray(item.getSdList(), Float.class);
                    float stepX = Float.parseFloat(item.getZdshj()) / suduVelList.size();
                    item.setSdChartLineDTO(BezierCurveUtil.calPoints(suduVelList, stepX, true));
                }
            });
        }
        return new AsyncResult<>(dataJkcrZhchshxEntities);
    }

    /**
     * 查询架空乘人重车上行空车下行测试数据列表
     * 
     * @param dataJkcrZhchshx 架空乘人重车上行空车下行测试数据
     * @return 架空乘人重车上行空车下行测试数据
     */
    @Override
    public List<DataJkcrZhchshx> selectDataJkcrZhchshxList(DataJkcrZhchshx dataJkcrZhchshx)
    {
        return dataJkcrZhchshxMapper.selectDataJkcrZhchshxList(dataJkcrZhchshx);
    }

    /**
     * 新增架空乘人重车上行空车下行测试数据
     * 
     * @param dataJkcrZhchshx 架空乘人重车上行空车下行测试数据
     * @return 结果
     */
    @Override
    public int insertDataJkcrZhchshx(DataJkcrZhchshx dataJkcrZhchshx)
    {
        dataJkcrZhchshx.setCreateTime(DateUtils.getNowDate());
        return dataJkcrZhchshxMapper.insertDataJkcrZhchshx(dataJkcrZhchshx);
    }

    /**
     * 批量新增架空乘人重车上行空车下行测试数据
     *
     * @param dataJkcrZhchshx
     */
    @Override
    public int batchDataJkcrZhchshx(List<DataJkcrZhchshx> dataJkcrZhchshx) {
        return dataJkcrZhchshxMapper.batchDataJkcrZhchshx(dataJkcrZhchshx);
    }

    /**
     * 修改架空乘人重车上行空车下行测试数据
     * 
     * @param dataJkcrZhchshx 架空乘人重车上行空车下行测试数据
     * @return 结果
     */
    @Override
    public int updateDataJkcrZhchshx(DataJkcrZhchshx dataJkcrZhchshx)
    {
        dataJkcrZhchshx.setUpdateTime(DateUtils.getNowDate());
        return dataJkcrZhchshxMapper.updateDataJkcrZhchshx(dataJkcrZhchshx);
    }

    /**
     * 修改架空乘人重车上行空车下行测试数据
     *
     * @param jkcrZhchshxChange 架空乘人重车上行空车下行测试数据
     * @return 结果
     */
    @Override
    public DataJkcrZhchshx modifyDataJkcrZhchshx(JkcrZhchshxChange jkcrZhchshxChange) {
        DataJkcrZhchshx dataJkcrZhchshx = dataJkcrZhchshxMapper.selectDataJkcrZhchshxByZhchshxId(jkcrZhchshxChange.getZhchshxId());
        List<Float> suduVelList = JSON.parseArray(dataJkcrZhchshx.getSdList(), Float.class);
        float zhdchsdOld = suduVelList.get(0);
        float zhdshjOld = suduVelList.size() * 0.1f;
        zhdchsdOld = zhdchsdOld == 0 ? 1 : zhdchsdOld;
        dataJkcrZhchshx.setZdshj(jkcrZhchshxChange.getZdshj());
        float addX = 0f;
        float xishu = Float.parseFloat(jkcrZhchshxChange.getZhdchsd()) / zhdchsdOld;
        float shjXishu = Float.parseFloat(jkcrZhchshxChange.getZdshj()) / zhdshjOld;
        float stepX = zhdshjOld / suduVelList.size() * shjXishu;
        List<Float> sdList = new ArrayList<>();
        List<Float> xVelList = new ArrayList<>(); List<Float> yVelList = new ArrayList<>();
        for (Float sdVel : suduVelList) {
            sdVel = sdVel * xishu;
            addX += stepX;
            xVelList.add(addX);
            yVelList.add(sdVel);
            sdList.add(sdVel);
        }
        dataJkcrZhchshx.setSdChartLineDTO(BezierCurveUtil.calPoints(xVelList, yVelList, true));
        dataJkcrZhchshx.setSdList(JSON.toJSONString(sdList));
        float zhdchsdNew = yVelList.get(0);
        dataJkcrZhchshx.setZhdjsd(String.format("%.2f", (zhdchsdNew / Float.parseFloat(dataJkcrZhchshx.getZdshj()))));
        dataJkcrZhchshx.setZhdjl(String.format("%.2f",(Float.parseFloat(dataJkcrZhchshx.getZhdjsd())
                * Float.parseFloat(dataJkcrZhchshx.getZdshj()) * Float.parseFloat(dataJkcrZhchshx.getZdshj()) * 0.5f)));
        updateDataJkcrZhchshx(dataJkcrZhchshx);
        return dataJkcrZhchshx;
    }

    /**
     * 变更数据状态
     *
     * @param dataTaskId
     * @return 结果
     */
    @Override
    public int updateDataJkcrZhchshxByDataTaskId(Long dataTaskId) {
        return dataJkcrZhchshxMapper.updateDataJkcrZhchshxByDataTaskId(dataTaskId);
    }

    /**
     * 变更数据状态
     *
     * @param zhchshxId
     * @return 结果
     */
    @Override
    public int updateDataJkcrZhchshxByZhchshxId(Long zhchshxId) {
        return dataJkcrZhchshxMapper.updateDataJkcrZhchshxByZhchshxId(zhchshxId);
    }

    /**
     * 批量删除架空乘人重车上行空车下行测试数据
     * 
     * @param zhchshxIds 需要删除的架空乘人重车上行空车下行测试数据主键
     * @return 结果
     */
    @Override
    public int deleteDataJkcrZhchshxByZhchshxIds(Long[] zhchshxIds)
    {
        return dataJkcrZhchshxMapper.deleteDataJkcrZhchshxByZhchshxIds(zhchshxIds);
    }

    /**
     * 删除架空乘人重车上行空车下行测试数据信息
     * 
     * @param zhchshxId 架空乘人重车上行空车下行测试数据主键
     * @return 结果
     */
    @Override
    public int deleteDataJkcrZhchshxByZhchshxId(Long zhchshxId)
    {
        return dataJkcrZhchshxMapper.deleteDataJkcrZhchshxByZhchshxId(zhchshxId);
    }
}
